% close all;
clear variables;

%% parameters
rootDir = ['..' filesep '191015_GeraldDoreenBob_Bats1'];

%% look for measurements
dirList = dir(rootDir);
dirList = dirList([dirList.isdir]);
dirList = dirList(3:end);

idxFTFit = strcmp({dirList.name},'FTFitData');

ptrSamples = 1:length(dirList);
ptrFTFit = ptrSamples(idxFTFit);
ptrSamples = ptrSamples(~idxFTFit);

flagSamples = false;
flagFTFit = false;
if ~isempty(ptrFTFit) && ~isempty(ptrSamples)
    fprintf('choose velocity analysis target:\n1\tsamples\n2\tFTFit\n');
    inp = input('select (1 / 2 / 1:2) ');
    if any(inp == 1)
        flagSamples = true;
    end
    if any(inp == 2)
        flagFTFit = true;
    end
elseif ~isempty(ptrFTFit)
    flagFTFit = true;
elseif ~isempty(ptrSamples)
    flagSamples = true;
end

%% FTFit Data
if flagFTFit
    dirListSub = dir([rootDir filesep dirList(ptrFTFit).name]);
    dirListSub = dirListSub(3:end);
    dirListSub = dirListSub([dirListSub.isdir]);
    
    % forbidden folders
    mask = strcmp({dirListSub.name},'ParaExport');
    dirListSub = dirListSub(~mask);
    dirListSub = natsortfiles(dirListSub);
    
    figure(1); clf;
    boxVec = [];
    groupVec = [];
    sampleNames = cell(1,length(dirListSub));
    for idxDirListSub = 1:length(dirListSub)
        load([rootDir filesep dirList(ptrFTFit).name filesep dirListSub(idxDirListSub).name filesep 'data.mat']);
        sampleNames{idxDirListSub} = sampleName;
        cellCounts = zeros(length(sampleData)+1,1);
        for idxSample = 1:length(sampleData)
            cellCounts(idxSample+1) = cellCounts(idxSample) + length(sampleData(idxSample).measData.cellData);
        end
        velVec = NaN(cellCounts(end),1);
        for idxSample = 1:length(sampleData)
            tmp = sampleData(idxSample).measData.cellData;
            tmp = [tmp.data];
            velVec(cellCounts(idxSample)+1:cellCounts(idxSample+1)) = [tmp.medianVelocity];
        end
        boxVec = [boxVec; velVec];
        groupVec = [groupVec; repmat(idxDirListSub,size(velVec))];
    end
    hbp = boxplot(boxVec,groupVec,'Widths',0.6,'Labels',sampleNames);
    adaptBox(hbp, [31 119 189]/255, 1.5);
    xlim(0.5*[-1 1]+[1 length(dirListSub)]);
    xtickangle(45);
    ylabel('Velocity (cm/s)');
    grid on;
    title(['FTFitData (' rootDir ')'], 'Interpreter','none');
    saveas(gcf,[rootDir filesep 'velocity_FTFit.png']);
end

%% samples data
if flagSamples
    for idxSample = 1:length(ptrSamples)
        dirListSub = natsortfiles(dir([rootDir filesep dirList(ptrSamples(idxSample)).name filesep 'Online' filesep 'M*_data_export.mat']));
        if isempty(dirListSub)
            continue
        end
        boxVec = [];
        groupVec = [];
        sampleNames = cell(1,length(dirListSub));
        for idxMeas = 1:length(dirListSub)
            load([rootDir filesep dirList(ptrSamples(idxSample)).name filesep 'Online' filesep dirListSub(idxMeas).name]);
            tmp = sample.measData.cellData;
            tmp = [tmp.data];
            if ~isempty(tmp)
                boxVec = [boxVec; [tmp.medianVelocity]'];
                groupVec = [groupVec; repmat(idxMeas,length(tmp),1)];
                sampleNames{idxMeas} = sprintf('M%d',sample.measData.number);
            else
%                 boxVec = [boxVec; nan];
            end
            
        end
        sampleNames = sampleNames(~cellfun('isempty',sampleNames));
        hbp = boxplot(boxVec,groupVec,'Widths',0.6,'Labels',sampleNames);
        adaptBox(hbp, [31 119 189]/255, 1.5);
        xlim(0.5*[-1 1]+[1 length(dirListSub)]);
        ylabel('Velocity (cm/s)');
        grid on;
        title([dirList(ptrSamples(idxSample)).name ' (' rootDir ')'], 'Interpreter','none');
        saveas(gcf,[rootDir filesep 'velocity_' dirList(ptrSamples(idxSample)).name '.png']);
    end
end

return
%% functions
function adaptBox(hbp, color, lw)
set(hbp(1:2,:),'LineStyle','-');
set(hbp(7,:),'MarkerEdgeColor',color,'Marker','.','MarkerSize',12);
for k = 1:size(hbp,1)
    set(hbp(k,:),'Color',color,'LineWidth',lw);
end
end